package com.bafarat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToe implements ActionListener        {
        JFrame window = new JFrame("Tic-Tac-Toe ");
        JMenuBar mnuMain = new JMenuBar();
        JMenuItem       mnuNewGame = new JMenuItem("New Game"), 
                                        mnuExit = new JMenuItem("Exit"); 
        JButton btnEmpty[] = new JButton[10];
        JPanel                 pnlNorth = new JPanel(),
                                pnlSouth = new JPanel(),
                                pnlPlayingField = new JPanel();
        final int winCombo[][] = new int[][]    {
                {1, 2, 3},                      {1, 4, 7},              {1, 5, 9},
                {4, 5, 6},                      {2, 5, 8},              {3, 5, 7},
                {7, 8, 9},                      {3, 6, 9},
        };
        final int X = 412, Y = 268, color = 250;
        boolean win = false;
        boolean btnEmptyClicked = false;
        String message;
        int turn = 1;
        int wonNumber1 = 1, wonNumber2 = 1, wonNumber3 = 1;
        public TicTacToe()      {    
                window.setSize(X, Y);
                window.setLocation(450, 260);
                window.setResizable(false);
                window.setLayout(new BorderLayout());
                window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
                pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
                pnlNorth.setBackground(new Color(color-20, color-20, color-20));
                pnlSouth.setBackground(new Color(color, color, color));
                mnuMain.add(mnuNewGame);
                mnuMain.add(mnuExit);
                mnuNewGame.addActionListener(this);
                mnuExit.addActionListener(this);
                pnlPlayingField.setLayout(new GridLayout(3, 3, 2, 2));
                pnlPlayingField.setBackground(Color.black);
                for(int i=1; i<=9; i++) {
                        btnEmpty[i] = new JButton();
                        btnEmpty[i].setBackground(new Color(220, 220, 220));
                        btnEmpty[i].setSize(70, 70);
                        btnEmpty[i].addActionListener(this);
                        pnlPlayingField.add(btnEmpty[i]);
                }
                pnlNorth.add(mnuMain);
                window.add(pnlNorth, BorderLayout.NORTH);
                window.add(pnlSouth, BorderLayout.CENTER);
                window.setVisible(true);
        }
        public void actionPerformed(ActionEvent click)  {
                Object source = click.getSource();
                for(int i=1; i<=9; i++) {
                        if(source == btnEmpty[i] && turn <      10)     {
                                btnEmptyClicked = true;
                                if(!(turn % 2 == 0))
                                        btnEmpty[i].setText("X");
                                else
                                        btnEmpty[i].setText("O");
                                btnEmpty[i].setEnabled(false);
                                turn++;
                        }
                }
                if(btnEmptyClicked)     {
                        checkWin();
                        btnEmptyClicked = false;
                }
                  if(source == mnuNewGame)        {
                      clearPanelSouth();
                      btnEmpty[wonNumber1].setBackground(new Color(220, 220, 220));
                      btnEmpty[wonNumber2].setBackground(new Color(220, 220, 220));
                      btnEmpty[wonNumber3].setBackground(new Color(220, 220, 220));
                      turn = 1;
                      for(int i=1; i<10; i++) {
                              btnEmpty[i].setText("");
                              btnEmpty[i].setEnabled(true);
                      }
                      win = false;
                      showGame();
              }
                else if(source == mnuExit)      {
                                System.exit(0);
                }
                pnlSouth.setVisible(false);
                pnlSouth.setVisible(true);
        }
        public void showGame()  {                                                                   
                clearPanelSouth();
                pnlSouth.setLayout(new BorderLayout());
                pnlSouth.add(pnlPlayingField, BorderLayout.CENTER);
                pnlPlayingField.requestFocus();
        }
        public void checkWin()  {           
                for(int i=0; i<8; i++)  {
                        if(
                                        !btnEmpty[winCombo[i][0]].getText().equals("") &&
                                        btnEmpty[winCombo[i][0]].getText().equals(btnEmpty[winCombo[i][1]].getText()) &&
                                        btnEmpty[winCombo[i][1]].getText().equals(btnEmpty[winCombo[i][2]].getText())
                                )       {
                                win = true;
                                wonNumber1 = winCombo[i][0];
                                wonNumber2 = winCombo[i][1];
                                wonNumber3 = winCombo[i][2];
                                btnEmpty[wonNumber1].setBackground(Color.white);
                                btnEmpty[wonNumber2].setBackground(Color.white);
                                btnEmpty[wonNumber3].setBackground(Color.white);
                                break;
                        }
                }
                if(win || (!win && turn>9))     {
                        if(win) {
                                if(turn % 2 == 0)
                                        message = "X has won!\nMade by Ammar Bafarat.";
                                else    
                                        message = "O has won!\nMade by Ammar Bafarat.";
                                win = false;
                        }       else if(!win && turn>9) {
                                message = "Both players have tied!\nBetter luck next time.\nMade by Ammar Bafarat.";
                        }
                                JOptionPane.showMessageDialog(null, message);
                        for(int i=1; i<=9; i++) {
                                btnEmpty[i].setEnabled(false);
                        }
                }
        }
        public void clearPanelSouth()   {       
                pnlSouth.remove(pnlPlayingField);
        }
        public static void main(String[] args)  {
                new TicTacToe();
        }
}